
import re
import os
import sys
import string
from pepe.txt import bigtxt as bt

zf = lambda x, y: string.zfill(str(x), y)
srj = lambda x, y: string.rjust(str(x), y)
slj = lambda x, y: string.ljust(str(x), y)
rnl = lambda x: re.sub('\n','',x)

#---------------------------------------------------------
def csv_hshow(inpFile, cDelimiter):
    """ csv_hshow(inpFile, cDelimiter) """
    fLineA, fLineB = bt.lineno(inpFile, 1), bt.lineno(inpFile, 2)
    print fLineA, fLineB
    fLineA, fLineB = rnl(fLineA), rnl(fLineB)
    iIndex = 0
    fLineBcols = string.split(fLineB, cDelimiter)
    print srj(inpFile,15), '\n','-'*80
    for colInx, col in enumerate(string.split(fLineA, cDelimiter)):
        iIndex += 1
        print zf(iIndex,3), slj(string.strip(col),20), \
              slj(string.strip(fLineBcols[colInx]),40)
    print '\n'*2
#---------------------------------------------------------
def csv_vshow(inFile, cDelimiter, cIndex):
    """ csv_vshow(inFile, cDelimiter, cIndex) """
    iIndex = 0
    for line in bigtxt.lines(inFile):
        item = string.split(line, cDelimiter)[int(cIndex)]
        iIndex += 1
        print zf(iIndex,3), slj(item,20), srj(inFile,15)
#---------------------------------------------------------

HELP_MSG = """      Purpose:    list csv header horizontally or vertically
    Syntax:     csv-show <data file> "<h_orizontally><delimiter>"
                csv-show <data file> "<v_ertically><delimiter>" <column no to list>
    Example:    csv-show data.dat "h|"
                csv-show data.dat "v|" 3"""

def main():
    if len(sys.argv) == 3 and sys.argv[2][:1] == 'h':
        csv_hshow(sys.argv[1], sys.argv[2][1:2])
    elif len(sys.argv) == 4 and sys.argv[2][:1] == 'v':
        csv_vshow(sys.argv[1], sys.argv[2][1:2], sys.argv[3])
    else:
        print HELP_MSG
        os.system('pause')

if __name__ == "__main__":
    main()
